//
//  HLMatchFateViewController.m
//  YueYue
//
//  Created by hulu001 on 2023/6/5.
//

#import "HLMatchFateViewController.h"
#import "HLMatchRequest.h"
#import "HLMatchFateCell.h"
#import "HLMatchFateTipView.h"


@interface HLMatchFateViewController ()

@property (nonatomic, strong) UIImageView *backgroundView;
@property (nonatomic, strong) UIView *bottomView;
@property (nonatomic, strong) QMUIButton *coinView;
@property (nonatomic, strong) QMUIButton *matchButton;

@end

@implementation HLMatchFateViewController


- (void)viewDidLoad {
    [super viewDidLoad];
    
    self.backgroundView = [[UIImageView alloc] initWithImage:UIImageMake(@"match_fate_background")];
    [self.view insertSubview:_backgroundView atIndex:0];
    [_backgroundView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.edges.mas_equalTo(0);
    }];
    
    
    self.request = [[HLMatchRequest alloc] initWithType:HLMatchTypeFateList params:@{@"pageNum":@"1", @"pageSize":@"20"}];
    self.request.delegate = self;
    [self requestInit];
}

- (void)viewWillAppear:(BOOL)animated
{
    [super viewWillAppear:animated];
    [self.tableView reloadData];
}

- (void)requestInit
{
    [self showEmptyView];
    [self.emptyView.loadingView startAnimating];
    [self.emptyView setTextLabelText:@"加载中..."];
    [self.emptyView setBackgroundColor:UIColorClear];
    [self.request start];
}


- (void)initTableView
{
    [super initTableView];
    
    self.view.backgroundColor = UIColorClear;
    self.tableView.backgroundColor = UIColorClear;
    [self.tableView registerClass:[HLMatchFateCell class] forCellReuseIdentifier:[HLMatchFateCell identifyCell]];
    
    self.bottomView = [[UIView alloc] init];
    _bottomView.backgroundColor = [UIColor qmui_colorWithHexString:@"#232A35"];
    _bottomView.layer.masksToBounds = YES;
    _bottomView.layer.cornerRadius = 20;
    [self.view addSubview:_bottomView];
    [_bottomView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.right.mas_equalTo(0);
        make.bottom.mas_equalTo(20);
        make.height.mas_equalTo(98 + (SafeAreaInsetsConstantForDeviceWithNotch.bottom));
    }];
    
    self.matchButton = [[QMUIButton alloc] init];
    [_matchButton setTitle:@"开始匹配" forState:UIControlStateNormal];
    [_matchButton setTitleColor:UIColorWhite forState:UIControlStateNormal];
    [_matchButton.titleLabel setFont:UIFontMediumMake(16)];
    [_matchButton setBackgroundColor:HLMainColor];
    [_matchButton setCornerRadius:22];
    [_matchButton setContentEdgeInsets:UIEdgeInsetsMake(12, 62, 12, 62)];
    [_matchButton addTarget:self action:@selector(matchAction) forControlEvents:UIControlEventTouchUpInside];
    [_bottomView addSubview:_matchButton];
    [_matchButton mas_makeConstraints:^(MASConstraintMaker *make) {
        make.right.mas_equalTo(-20);
        make.top.mas_equalTo(16);
    }];
    
    
    self.coinView = [[QMUIButton alloc] init];
    [_coinView setImage:UIImageMake(@"home_flow_coin") forState:UIControlStateNormal];
    [_coinView setTitle:@"199/分钟" forState:UIControlStateNormal];
    [_coinView setTitleColor:UIColorWhite forState:UIControlStateNormal];
    [_coinView.titleLabel setFont:UIFontMediumMake(14)];
    [_coinView setImagePosition:QMUIButtonImagePositionLeft];
    [_coinView setSpacingBetweenImageAndTitle:4];
    [_bottomView addSubview:_coinView];
    [_coinView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.mas_equalTo(20);
        make.centerY.equalTo(_matchButton);
    }];
}


- (void)matchAction
{
    MJWeakSelf;
    [HLMatchFateTipView showWithLabel:nil callback:^(BOOL canMatch) {
        if (canMatch){
            [weakSelf matchAction];
        }
    }];
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    return self.datasources.count;
}

-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
    HLMatchFateCell *cell = [tableView dequeueReusableCellWithIdentifier:[HLMatchFateCell identifyCell]];
    if (self.datasources.count <= indexPath.row){
        return cell;
    }
    HLFateModel *model = self.datasources[indexPath.row];
    model.index = indexPath.row;
    cell.model = model;
    return cell;
}


- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
}


- (void)requestFinished:(__kindof YTKBaseRequest *)request
{
    [super requestFinished:request];
    
    HLMatchRequest *fateRequest = (HLMatchRequest *)request;
    NSArray *fateUsers = [fateRequest fateList];
    if (fateUsers.count > 2){
        NSInteger rowCount = fateUsers.count / 2;
        
        HLFateModel *fLineModel = [[HLFateModel alloc] init];
        fLineModel.users = [fateUsers subarrayWithRange:NSMakeRange(0, rowCount)];
        
        HLFateModel *sLineModel = [[HLFateModel alloc] init];
        sLineModel.users = [fateUsers subarrayWithRange:NSMakeRange(rowCount, (fateUsers.count - rowCount))];

        [self.datasources addObjectsFromArray:@[fLineModel,sLineModel]];
        [self.tableView reloadData];
    }
    if (fateUsers.count == 0){
        [self.emptyView setLoadingViewHidden:YES];
        [self.emptyView setImage:UIImageMake(@"empty_icon_6")];
        [self.emptyView setTextLabelText:@"闪聊空空如也"];
        self.emptyView.textLabelTextColor = UIColorWhite;
    } else {
        [self hideEmptyView];
    }
    
    [self.tableView reloadData];
}

- (void)requestFailed:(__kindof YTKBaseRequest *)request
{
    [super requestFailed:request];
}

- (void)setupNavigationItems
{
    [super setupNavigationItems];
    self.title = @"缘分速配";
}

- (UIImage *)qmui_navigationBarBackgroundImage
{
    return [UIImage qmui_imageWithColor:UIColorClear];
}

- (nullable UIColor *)qmui_navigationBarTintColor
{
    return UIColorWhite;
}

- (nullable UIColor *)qmui_titleViewTintColor
{
    return UIColorWhite;
}

- (BOOL)shouldHideKeyboardWhenTouchInView:(UIView *)view
{
    return YES;
}

- (UIStatusBarStyle)preferredStatusBarStyle
{
    return UIStatusBarStyleLightContent;
}

@end
